/* main.js */

{
    let href = location.href,
        search = location.search;
    //廣告默認跳轉，解決IOS9以下帶#會被吞掉問題（c.8891.com.tw/m/show/?/topic/27/9803）
    if (search && search.split('?/')[1]) {
        let link = href.replace('?/', '');
        history.replaceState(null, '', link);
    }else if (search && href.indexOf(search + '#/') != -1) { //解決帶#的廣告跳轉問題
        let link = href.replace(search + '#/', '');
        history.replaceState(null, '', link + search);
    };
     //舊的帶#的鏈接跳轉為不帶#的新鏈接
    if (href.indexOf('show/#/') != -1) {
        let link = href.replace('show/#', 'show');
        history.replaceState(null, '', link);
    }else if (href.indexOf('show/#') != -1) {
        let link = href.replace('show/#', 'show/');
        history.replaceState(null, '', link);
    };
};

{
    if(location.protocol !== "https:" && location.host.indexOf("dev") == -1){
        location.href = location.href.replace(/^http/ig,'https');
    }
};

{
    //從其他地方進入網站的，插入一條歷史記錄，讓用戶點返回回到首頁
    var refe = document.referrer;
    if (refe == '' || refe.indexOf('8891.com.tw/m') == -1) {
        var _href = location.href;
        history.replaceState({}, '', location.origin+'/m/');
        history.pushState({}, '', _href);
    };
};


import React from 'react';
import { render } from 'react-dom';
import { Provider } from 'react-redux'
import routes from './src/routes'
import { Router, browserHistory, applyRouterMiddleware, match } from 'react-router';
import { syncHistoryWithStore } from 'react-router-redux'
import { useScroll } from 'react-router-scroll'
import configStore from './src/store/configStore'
import 'antd-mobile/dist/antd-mobile.min.css'
import './src/resource/css/main.less'
import './src/utils/htmlFontSize'
import { getOs } from './src/utils'
import useBasename from 'history/lib/useBasename'
import Api from './src/utils/api'
import ReactGA from 'react-ga'
import GoogleTagManager from './src/utils/gtm'


const store = configStore();
const baseurl = '/m'
global.Api = Api;
global.historyUrl = [];    //存儲歷史記錄
global.ReactGA = ReactGA;	//ga
global.getOs = getOs;


ReactGA.initialize('UA-10127157-12');

function appHistory(history) {
	return useBasename(() => history)({
		basename: baseurl
	});
}
const historyFn = syncHistoryWithStore(appHistory(browserHistory),store);


function logPageView() {
	historyUrl.push(location.href); //存儲歷史記錄
    if (historyUrl.length > 10) {
        historyUrl.shift(); //历史记录太多的话删掉一点，数量自己控制
    }
    
	// ReactGA.ga('send', 'pageview', global.location.href.replace(Api.host, ''));
	if (location.pathname.indexOf('articledetails') == -1 && location.pathname.indexOf('m/show/topic') == -1) {
        dataLayer.push({'event': 'pageChange', 'virtualPageUrl': global.location.pathname + global.location.search});
	}
}

render(
	<div>
		<Provider store={store}>
			<Router routes={routes} history={historyFn} onUpdate={logPageView} render={applyRouterMiddleware(useScroll())} />
		</Provider>
		<GoogleTagManager gtmId='GTM-N47BNPG' />
	</div>
	,
	document.getElementById('container')
);